<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: relative;
            top: 10px;
            left: 10px;
            width: 100px;
            height: 100px;
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <button>1111</button>
</body>
<script>
    var box = document.getElementsByClassName('box')[0];
    var top1 = [];
    var left1 = [];
    box.onmousedown = function (e) {
        //获取当前点在盒子上的坐标
        var y = e.pageY - box.offsetTop;
        var x = e.pageX - box.offsetLeft;

        //获取移动坐标
        window.onmousemove = function (e1) {
            var t = e1.pageY - y;
            var l = e1.pageX - x;
            box.style.top = t + 'px';
            box.style.left = l + 'px';

            //将坐标追加到第一个位置
            top1.unshift(t);
            left1.unshift(l);
        }
        box.onmouseup = function (e2) {
            //当鼠标抬起时,去除事件(不让盒子跟着鼠标移动)
            window.onmousemove = null
        }
    }
    var but = document.getElementsByTagName('button')[0];
    but.onclick = function (e) {
        var i = 0;
        var interid = setInterval(
            function () {
                box.style.top = top1[i] + 'px';
                box.style.left = left1[i] + 'px';
                i++;

                //如果i>=最大索引,清除定时器
                if (i >= top1.length) {
                    clearInterval(interid);
                    box.style.top = '10px';
                    box.style.left = '10px';
                    top1=[];
                    left1=[];
                }
            },10
        )
    }
</script>

</html>